#include "tplconfig.h"
#include <QTextStream>


#define TPL_STATE_NO_STATE (-1)
#define TPL_STATE_INPUT_FILES 0
#define TPL_STATE_TEMPLATE_FILES 1
#define TPL_STATE_OUTPUT_FILE 2

TplConfig* TplConfig::m_config = 0;

TplConfig* TplConfig::getConfig() {
    //No need for mutex. It is not used multithreadead.
    if(m_config == 0) {
        m_config = new TplConfig();
    }

    return m_config;
}

TplConfig::TplConfig(QObject *parent) :
    QObject(parent),m_noErr(0),m_noWarn(0),m_noDebug(0) {
}

bool TplConfig::configFileExists() const {
    return false;
}

bool TplConfig::noErr() const {
    return this->m_noErr;
}

bool TplConfig::noWarn() const {
    return this->m_noWarn;
}

bool TplConfig::noDebug() const {
    return this->m_noDebug;
}

int TplConfig::parseConfig(const QIODevice &inputSource) {
    return 0;
}

/**
  Command line is :
  *                 -i [inputFile(s)]  -- json input file(s).
  *                 -o [outputFile]    -- output file.
  *                 -t [templateFile(s)]  -- template file to be combined with input and result output.
  */
int TplConfig::parseCommandLine(int argc,char *argv[]) {
    int state = -1;
    this->inputList.clear();
    this->templateList.clear();
    this->outputFile = "";

    if(argc < 4) {
        qFatal("Usage: %s"," -i [inputFile(s)] -o [outputFile] -t [templateFile(s)]" );
        return 0;
    }

    for(int i=1; i<argc; i++) {

        if(strcmp("-i",argv[i]) == 0) {
            state = TPL_STATE_INPUT_FILES;
        } else if(strcmp("-t",argv[i]) == 0) {
            state = TPL_STATE_TEMPLATE_FILES;
        } else if(strcmp("-o",argv[i]) == 0) {
            state = TPL_STATE_OUTPUT_FILE;
        } else {

            if(state == TPL_STATE_NO_STATE) {
                qFatal( "Usage: %s"," -i [inputFile(s)] -o [outputFile] -t [templateFile(s)]" );
                return 0;
            } else if(state == TPL_STATE_INPUT_FILES) {
                this->inputList.append(QString(argv[i]));
            } else if (state == TPL_STATE_TEMPLATE_FILES) {
                this->templateList.append(QString(argv[i]));
            } else if(state == TPL_STATE_OUTPUT_FILE) {
                this->outputFile = argv[i];
            }

        }
    }

    if(this->inputList.size() <1 ) {
        qFatal("Application requires at least one input file.");
        return 0;
    }

    if(this->templateList.size() <1 ) {
        qFatal("Application requires at least one template file.");
        return 0;
    }

#ifdef TPL_DEBUG
    QStringList::iterator i;

    qDebug("Input files: ");
    for (i = this->inputList.begin(); i != this->inputList.end(); ++i) {
        qDebug() << *i;
    }
    qDebug() << endl;

    qDebug("Template files: ");
    for (i = this->templateList.begin(); i != this->templateList.end(); ++i) {
        qDebug() << *i;
    }
    qDebug() << endl;

    qDebug() << "Output file is : " << this->outputFile << endl;
#endif

    return this->inputList.size() + this->templateList.size() + 1;
}
